//
//  BaseTableView.m
//
//
//

#import "BaseTableView.h"


@implementation BaseTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        [self _createView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self _createView];
    }
    return self;
}


- (void)_createView
{
    self.dataSource = self;
    self.delegate = self;
    self.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:243.0/255.0 blue:246.0/255.0 alpha:1];
    //    self.backgroundView = nil;
    
    
    //下拉刷新  0 - self.bounds.size.height
    _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0 - 200, self.bounds.size.width, 200)];
    _refreshHeaderView.delegate = self;
    _refreshHeaderView.backgroundColor = [UIColor clearColor];
    [self addSubview:_refreshHeaderView];
    //    _refreshHeaderView.hidden = YES;
    //    _refreshHeaderView.alpha = 0.0;
    
    _refreshHeader = YES;
    
    //上拉加载
    _refreshFooterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _refreshFooterButton.frame = CGRectMake(10, 15, kScreenWidth - 20, 36);
    _refreshFooterButton.backgroundColor = [UIColor clearColor];
    [_refreshFooterButton setTitle:@"..." forState:UIControlStateNormal];
    [_refreshFooterButton addTarget:self
                             action:@selector(loadMoreAction)
                   forControlEvents:UIControlEventTouchUpInside];
    _refreshFooterButton.backgroundColor = [UIColor whiteColor];
    [_refreshFooterButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _refreshFooterButton.layer.cornerRadius = 5;
    _refreshFooterButton.hidden = YES;
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 66)];
    [footerView addSubview:_refreshFooterButton];
    self.tableFooterView = footerView;
    
    //    [self.tableFooterView addSubview:_refreshFooterButton];
    
    UIActivityIndicatorView *loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loadView.frame = CGRectMake(90, 10, 20, 20);
    [loadView stopAnimating];
    //    loadView.hidden = YES;
    loadView.tag = 200;
    [_refreshFooterButton addSubview:loadView];
    
    
    
}

- (void)setRefreshHeader:(BOOL)refreshHeader
{
    _refreshHeader = refreshHeader;
    if (!_refreshHeader) {
        [_refreshHeaderView removeFromSuperview];
    }
}

- (void)setIsLastPage:(BOOL)isLastPage
{
    _isLastPage = isLastPage;
    if (!isLastPage) {
        [_refreshFooterButton setTitle:@"..." forState:UIControlStateNormal];
        _refreshFooterButton.enabled = YES;
    }else
    {
        [_refreshFooterButton setTitle:@"加载完成" forState:UIControlStateNormal];
        _refreshFooterButton.enabled = NO;
    }
    
    UIActivityIndicatorView *loadView = (UIActivityIndicatorView *)[_refreshFooterButton viewWithTag:200];
    [loadView stopAnimating];
    
    _uploading = NO;
    
}


- (void)loadMoreAction
{
    _uploading = YES;
    _refreshFooterButton.enabled = NO;
    [_refreshFooterButton setTitle:@"正在加载" forState:UIControlStateNormal];
    UIActivityIndicatorView *loadView = (UIActivityIndicatorView *)[_refreshFooterButton viewWithTag:200];
    loadView.hidden = NO;
    [loadView startAnimating];
    
    if (self.upBlock) {
        self.upBlock();
    }
    
}


- (void)setData:(NSArray *)data
{
    if (_data != data) {
        _data = data;
        if (data.count > 0) {
            _refreshFooterButton.hidden = NO;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    _reloading = YES;
    
}

- (void)doneLoadingTableViewData{
    if (_reloading) {
        _reloading = NO;
        [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
        //        [UIView animateWithDuration:0.3 animations:^{
        //            //NSLog(@"sddfgh   %f",_refreshHeaderView.superview.frame.origin.y);
        //            if (_refreshHeaderView.superview.frame.origin.y == 0) {
        //                _refreshHeaderView.alpha = 0;
        //                //NSLog(@"sddfgh");
        //            }
        //        }];
    }
}


#pragma mark - UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    if (scrollView.frame.origin.y == 0) {
        CGFloat f = (-64 - scrollView.contentOffset.y)/65;
        _refreshHeaderView.alpha = f;
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    
    
    if (_uploading == NO) {
        //偏移量＋视图高度 ＝ 内容高度
        CGFloat h1 = scrollView.contentOffset.y + CGRectGetHeight(scrollView.bounds);
        CGFloat h2 = scrollView.contentSize.height;
        
        if (h1 - h2 > 30 && !_isLastPage && h2 - scrollView.height > 30) {
            [self loadMoreAction];
        }
        
    }
}


#pragma mark - EGORefreshTableHeaderDelegate Methods
//下拉到一定距离，手指放开时调用
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    
    [self reloadTableViewDataSource];
    //    _reloading = YES;
    
    
    //    //停止加载，弹回下拉
    //    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
    if (self.pullBlock) {
        self.pullBlock();
    }
    
}

//返回当前刷新的状态
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}

//取得下拉刷新的时间
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
    
}



- (void)hiddenfootView{
    _refreshFooterButton.hidden = YES;
    self.userInteractionEnabled = NO;
}
- (void)showfootView{
    _refreshFooterButton.hidden = NO;
    self.userInteractionEnabled = YES;
}
@end
